<?php
import('@.Action.PublicAction');
class PartyAction extends Action
{
    function _initialize() 
    {
       if(!isset($_SESSION['user_right'])) {
			$this->redirect('login','Public');
        }
        parent::_initialize();
    }
    	
	function index()
	{
		//如果是Action的初始入口，就清空$_SESSION["map"];
		if($_REQUEST["m"]==$this->name&&$_REQUEST["a"]==""&&count($_REQUEST)==2){
			$_SESSION["map"]=null;
		}
		//列表过滤器，生成查询Map对象
        $map = $this->_search('PartyView');	 
        if(method_exists($this,'_filter')) {
            $this->_filter($map);
        }
		$model        = D('PartyView');
        if(!empty($model)) {
        	$this->_list($model,$map);
        }
		//装载搜索字段
		$tabName="Party";
		$modelComment=D("Comment");
		$fieldList=$modelComment->findAll("`TABLE`='".$tabName."' and `RELID`='0' and `FIELD`!='xh'");
		$baseList=$modelComment->findAll("`TABLE`='studentinfo' and `RELID`='0' and `FIELD` in ('xh','xm','bj')");
		$fieldList=array_merge($baseList,$fieldList);
		$this->assign("fieldList",       $fieldList);			
		$this->display();
        return;
	}
    /**
     +----------------------------------------------------------
     * 根据表单生成查询条件
     * 进行列表过滤
     +----------------------------------------------------------
     * @access protected 
     +----------------------------------------------------------
     * @param string $name 数据对象名称 
     +----------------------------------------------------------
     * @return HashMap
     +----------------------------------------------------------
     * @throws ThinkExecption
     +----------------------------------------------------------
     */
    protected function _search($name='') 
    {
        //生成查询条件
		if(empty($name)) {
			$name	=	$this->name;
		}
		$model	=	D($name);
		$map	=	array();		//加入SESSION查询条件
		if(isset($_SESSION["map"]))
		{
			foreach($_SESSION["map"] as $key=>$val){
				$map[$key]	=	$val;
			}
		}
		//加入查询条件
		if(!empty($_POST['searchValue'])){
			$map[$_POST['searchKey']]	=	$_POST['searchValue'];
		}
		
		
		//加入权限
		//$map["Studentinfo.bj"]="2004级会计学4";
		if ($_SESSION['user_right']=="6"){
			//$map['dm_yx']=$_SESSION['dm_yx'];
			//$map['dm_zy']=$_SESSION['dm_zy'];
			$map['dm_bj']=$_SESSION['dm_bj'];			
		}
		//保存查询条件到session中
		$_SESSION["map"]=$map;
		return $map;
    }	
    /**
     +----------------------------------------------------------
     * 根据表单生成查询条件
     * 进行列表过滤
     +----------------------------------------------------------
     * @access protected 
     +----------------------------------------------------------
     * @param Model $model 数据对象 
     * @param HashMap $map 过滤条件 
     * @param string $sortBy 排序 
     * @param boolean $asc 是否正序 
     +----------------------------------------------------------
     * @return void
     +----------------------------------------------------------
     * @throws ThinkExecption
     +----------------------------------------------------------
     */
    protected function _list($model,$map,$sortBy='',$asc=true) 
    {
        //排序字段 默认为主键名
        if(isset($_REQUEST['order'])) {
            $order = $_REQUEST['order'];
        }else {
            $order = !empty($sortBy)? $sortBy: "xh";
        }
        //排序方式默认按照倒序排列
        //接受 sost参数 0 表示倒序 非0都 表示正序
        if(isset($_REQUEST['sort'])) {
            $sort = $_REQUEST['sort']?'asc':'desc';
        }else {
            $sort = $asc?'asc':'desc';
        }

		//map中的查询条件导出
		import("ORG.Util.HashMap");
		$maptmp=new HashMap();
        /**/
		foreach ($map as $key=>$val){
			$maptmp->put($key,array('like',"%".$val."%"));
		}


		//取得满足条件的记录数
        $count      = $model->count($maptmp);
		if($count>0) {
			import("ORG.Util.Page");
			//创建分页对象
			if(!empty($_REQUEST['listRows'])) {
				$listRows  =  $_REQUEST['listRows'];
			}else {
				$listRows  =  '';
			}
			$p          = new Page($count,$listRows);
			//分页查询数据
			$voList     = $model->findAll($maptmp,'*',$order.' '.$sort,$p->firstRow.','.$p->listRows);

			//分页显示
			$page       = $p->show();
			//列表排序显示
			$sortImg    = $sort ;                                   //排序图标
			$sortAlt    = $sort == 'desc'?'升序排列':'倒序排列';		//排序提示
			$sort       = $sort == 'desc'? 1:0;                     //排序方式
			//模板赋值显示
			$this->assign('list',       $voList);
			$this->assign('sort',       $sort);
			$this->assign('order',      $order);
			$this->assign('sortImg',    $sortImg);
			$this->assign('sortType',   $sortAlt);
			$this->assign("page",       $page);
		}
        return ;
    }
	

    /**
     +----------------------------------------------------------
     * 默认删除操作
     +----------------------------------------------------------
     * @access public 
     +----------------------------------------------------------
     * @return string
     +----------------------------------------------------------
     * @throws ThinkExecption
     +----------------------------------------------------------
     */
    public function delete()
    {
        //删除指定记录
        $model        = D('Party');
        if(!empty($model)) {
            $id         = $_REQUEST['id'];
            if(isset($id)) {
                if($model->delete($id)){
                    $this->success(L('_DELETE_SUCCESS_'));
                }else {
                    $this->error(L('_DELETE_FAIL_'));
                }        	
            }else {
                $this->error('非法操作');
            }        	
        }
    }

    /**
     +----------------------------------------------------------
     * 默认添加操作
     +----------------------------------------------------------
     * @access public 
     +----------------------------------------------------------
     * @return string
     +----------------------------------------------------------
     * @throws ThinkExecption
     +----------------------------------------------------------
     */
	function add() 
	{
		$tabName="Party";
		$modelComment=D("Comment");
		$voList=$modelComment->findAll("`TABLE`='".$tabName."'");
		

		//以ID为标号的全部数据组
		foreach($voList as $v1){
			$Fields[$v1['ID']]=$v1;
		}
		//以ID为标号的代码数据组,排除YX,ZY,BJ
		foreach($Fields as $v2){
			if($v2['ISCODE']==1&$v2['RELID']!=0){
					$tmp=$v2;
					$tmp['RELNAME']=$Fields[$v2['RELID']]['FIELD'];
					$codeModel=D("Code");
					$tmp['codes']=$codeModel->findAll("`COMMENTID`='".$v2['ID']."'");
					$codeFields[$v2['ID']]=$tmp;
			}
		}
	
		//加入选项		
		foreach($voList as $voListTmp){
			if($voListTmp['ISCODE']==1&&$voListTmp['RELID']!=0){
				$voListTmp['codes']=$codeFields[$voListTmp['ID']]['codes'];
				$voListTmp['RELNAME']=$codeFields[$voListTmp['ID']]['RELNAME'];
			}
			$voList2[]=$voListTmp;
		}
	
		$this->assign("list",$voList2);	
		// 添加内容的页面 
		$this->display(); 
	} 
   // 检查该学生是否存在 
    public function checkStuExists() { 
        if(!empty($_POST['xh'])) { 
            $Dao    =    D("Studentinfo"); 
			$vo=$Dao->getByXh($_POST['xh']);
            if($vo['xh']) { 
                $this->success('该学号存在,可以加入!<br>院系：'.$vo['yx']." 专业：".$vo['zy']." 班级：".$vo['bj']); 
            }else{ 
                $this->error('该学号不存在基本信息库中!'); 
            } 
        }else{ 
            $this->error('学号是必须的'); 
        } 
    }  
	function insert() 
	{ 
        if(!empty($_POST['xh'])) { 
            $Dao    =    D("Studentinfo"); 
			$vo=$Dao->getByXh($_POST['xh']);
            if(!$vo['xh']) { 
                 $this->error('该学号不存在基本信息库中!'); 
				 exit();
            } 
        }else{ 
            $this->error('学号是必须的');
			exit(); 
        } 	
		// 保存添加的数据
		$Dao = D("Party"); 
		$vo = $Dao->create(); 
		if(false === $vo) { 
	 		$this->error($Dao->getError()); 
		} 
 
		$rs = $Dao->add(); 
		if($rs){ 
			$this->assign("jumpUrl",__URL__); 
			$this->success("添加数据成功!"); 
		}else{ 
			$this->error("添加数据失败"); 
		} 
	}
    /**
     +----------------------------------------------------------
     * 默认编辑操作
     +----------------------------------------------------------
     * @access public 
     +----------------------------------------------------------
     * @return string
     +----------------------------------------------------------
     * @throws ThinkExecption
     +----------------------------------------------------------
     */
	function edit() 
	{ 
		$tabName="Party";
		$modelComment=D("Comment");
		$voList=$modelComment->findAll("`TABLE`='".$tabName."'");
		

		//以ID为标号的全部数据组
		foreach($voList as $v1){
			$Fields[$v1['ID']]=$v1;
		}
		//以ID为标号的代码数据组,排除YX,ZY,BJ
		foreach($Fields as $v2){
			if($v2['ISCODE']==1&$v2['RELID']!=0){
					$tmp=$v2;
					$tmp['RELNAME']=$Fields[$v2['RELID']]['FIELD'];
					$codeModel=D("Code");
					$tmp['codes']=$codeModel->findAll("`COMMENTID`='".$v2['ID']."'");
					$codeFields[$v2['ID']]=$tmp;
			}
		}
	
		//加入选项		
		foreach($voList as $voListTmp){
			if($voListTmp['ISCODE']==1&&$voListTmp['RELID']!=0){
				$voListTmp['codes']=$codeFields[$voListTmp['ID']]['codes'];
				$voListTmp['RELNAME']=$codeFields[$voListTmp['ID']]['RELNAME'];
			}
			$voList2[]=$voListTmp;
		}
	
						
		$this->assign("list",$voList2);
		// 编辑页面 
		$Dao = D("Party"); 
		$vo = $Dao->find("xh=".$_GET["id"]); 
		if($vo === false) 
		{ 
			$this->assign("jumpUrl",__URL__); 
			$this->error("Non-existed record"); 
		} 
		 
		$this->assign("vo",$vo); 
		$this->display(); 
	} 
	
	function update() 
	{ 
		// 保存编辑过的数据 
		$Dao = D("Party"); 
		$vo = $Dao->create("","edit");
		if(false === $vo) { 
	 		$this->error($Dao->getError()); 
		} 
 
		$rs = $Dao->save(); 
		if($rs){ 
			$this->assign("jumpUrl",__URL__); 
			$this->success("保存修改成功!"); 
		}else{ 
			$this->error("保存修改失败"); 
		} 
	}
    /**
     +----------------------------------------------------------
     * 默认导出操作
     +----------------------------------------------------------
     * @access public 
     +----------------------------------------------------------
     * @return string
     +----------------------------------------------------------
     * @throws ThinkExecption
     +----------------------------------------------------------
     */	
	function exportData() {
		set_time_limit(0);
		
		//使用哪个模型和表字段注释
		$modelName="PartyView";
		$tabName="Party";
		//准备导出数据
		$model= D($modelName);
        $map = $this->_search('PartyView');	//列表过滤器，生成查询Map对象 		
		import("ORG.Util.HashMap");//map中的查询条件导出
		$maptmp=new HashMap();
        /*map中的查询条件导出*/
		foreach ($map as $key=>$val){
			$maptmp->put($key,array('like',"%".$val."%"));
		}	
		$list  = $model->findAll($maptmp,'*','');

		$modelComment=D("Comment");
		$vo=$modelComment->findAll("`TABLE`='".$tabName."'");
		//start 附加字段
		$fieldsize[]=20;
		$fieldname[]="姓名";
		$fieldDm[]="xm";	
		$fieldsize[]=20;
		$fieldname[]="考生号";
		$fieldDm[]="ksh";			
		$fieldsize[]=20;
		$fieldname[]="出生日期";
		$fieldDm[]="birthday";	
		$fieldsize[]=20;
		$fieldname[]="性别";
		$fieldDm[]="xb";
		$fieldsize[]=60;
		$fieldname[]="班级";
		$fieldDm[]="bj";
		$fieldsize[]=22;
		$fieldname[]="身份证号";
		$fieldDm[]="sfzh";									
		//end 附加字段
		
		//准备字段和长度,字段中文名称
		foreach($vo as $tmp){
			$fieldsize[]=$tmp['LENGTH'];
			$fieldname[]=$tmp['DBFNAME'];
			$fieldDm[]=$tmp['FIELD'];
		}
		//准备导出数据
		foreach($list as $tmp1){
			$arr="";
			foreach($fieldDm as $tmp2){
				$arr[]=$tmp1[$tmp2];
				//echo $tmp1[$tmp2]."<br>";
			}
			$field[]=$arr;
		}
		//清除过期的缓存文件(就是两分钟以前的)
		$files=glob("../Public/Uploads/*.dbf");
		foreach($files as $v){
			if(time()>(filemtime($v)+1200)){
				@unlink($v);
			}
		}
		
		//导入DBF类
        import("ORG.Io.Dbf");
		//准备数据，字段和字段大小
		//$field=array(array("2005551033","无问林","男"),array("2005551034","房磊","男"));
		
/*		$arr="";
		$arr[]="2005551033";
		$arr[]="无问林";
		$arr[]="男";
		$field[]=$arr;
		
		$arr="";
		$arr[]="2005551033";
		$arr[]="无问林";
		$arr[]="男";
		$field[]=$arr;
		
		$fieldsize[]=20;
		$fieldsize[]=30;
		$fieldsize[]=2;
		//$fieldsize=array(20,30,2);
		$fieldname[]="无问林和房11";
		$fieldname[]="无问林房22";
		$fieldname[]="无问林房33";*/
		//$fieldname=array("无问林房11","无问林房22","无问林房33");
		//写DBF文件

		$outfile=new outdbf('../Public/Uploads/'.time().".dbf",$field,$fieldsize,$fieldname,1);/*发送响应*/
		header('Content-Type: text/html; charset=UTF-8');	
		echo "<div align='center'><a href='http://".$_SERVER['HTTP_HOST']."/".str_replace("../","",$outfile->file)."'>下载地址</a></div>";
		//$this->display("edit");
/*		//读文件
		$fd=fopen($outfile->file,"rb");
		$filecontent=fread($fd,filesize($outfile->file));	
		fclose($fd);
		//下载文件
		header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
		header('Content-type: application/dbf');
		header('Content-Disposition: attachment; filename="data_'.$tabName.'_'.time().'.dbf"');
		//发送数据
		echo $filecontent;*/

	}	 
}
?>